//
//  MovieDetiledViewController.m
//  UI_APP_Project_Bean
//
//  Created by dllo on 16/6/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieDetiledViewController.h"
#import "MovieModel.h"
#import "DetiledMovieModel.h"
#import "CinPicTableViewCell.h"
#import "CineLabelTableViewCell.h"
#import "GetHeightTools.h"


@interface MovieDetiledViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSDictionary *detailedDic;
@property(nonatomic, retain)DetiledMovieModel *detiledModel;

@end

@implementation MovieDetiledViewController

- (void)dealloc
{
    [_detiledModel release];
    [_detailedDic release];
    [_movieModel release];

    [super dealloc];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =  [UIColor whiteColor];
    [self config];
    
    [self handleData];
    
    [self creatTableView];
}

-(void)config{
    self.navigationItem.title = self.movieModel.movieName;
    
    self.detailedDic = [NSDictionary dictionary];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"m%@", self.movieModel.movieId] ofType:@".txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    self.detailedDic = [dic objectForKey:@"result"];
    self.detiledModel = [[DetiledMovieModel alloc]init];
     [self.detiledModel setValuesForKeysWithDictionary:self.detailedDic];

    NSLog(@"%@", self.detiledModel);
    
    
}


-(void)creatTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [tableView registerClass:[CinPicTableViewCell class] forCellReuseIdentifier:@"picMovCell"];
    [tableView registerClass:[CineLabelTableViewCell class] forCellReuseIdentifier:@"labelMovCell"];
    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        CinPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"picMovCell"];
        cell.model = self.movieModel;
        cell.detiModel = self.detiledModel;
        return cell;
    }
    CineLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"labelMovCell"];
    cell.detiledModel = self.detiledModel;
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return [GetHeightTools heightWith:self.detiledModel.actors] + 210 ;
    }
    return [GetHeightTools heightWith:self.detiledModel.plot_simple] + 80;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
